package cn.wolfcode.car.util;

import cn.wolfcode.car.domain.Employee;
import cn.wolfcode.car.domain.Permission;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContextUtil {
    private static final String EMPLOYEE_SESSION_NAME = "EMPLOYEE_IN_SESSION";
    private static final String PERMISSIONS_SESSION_NAME = "PERMISSIONS_IN_SESSION";

    public static HttpSession getSession(){
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    public static Employee getCurrentEmployee(){
        return (Employee) getSession().getAttribute(EMPLOYEE_SESSION_NAME);
    }

    public static void setCurrentEmployee(Employee employee){
        getSession().setAttribute(EMPLOYEE_SESSION_NAME, employee);
    }

    public static List<Permission> getCurrentPermissions(){
        return (List<Permission>) getSession().getAttribute(PERMISSIONS_SESSION_NAME);
    }

    public static void setCurrentPermissions(List<Permission> permissions){
        getSession().setAttribute(PERMISSIONS_SESSION_NAME, permissions);
    }

}
